package com.bluej.web.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.mortbay.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Servlet Filter implementation class AothorFilter
 */
@WebFilter("/servlet/*")
public class AothorFilter implements Filter {
	
	private Logger logger = LoggerFactory.getLogger(AothorFilter.class);

    /**
     * Default constructor. 
     */
    public AothorFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
		request.setCharacterEncoding("utf-8");
		HttpServletRequest req = (HttpServletRequest) request; 
		HttpSession session = req.getSession();
		logger.debug("sessionId:"+session.getId());
		if("true".equals(String.valueOf(session.getAttribute("login")))||req.getRequestURI().contains("servlet/login")){
			chain.doFilter(request, response);
		}else{
			logger.info("--未登录--");
			req.getRequestDispatcher("unlogin.jsp").forward(request, response);
		}
		response.setContentType("text/html; charset=utf-8");
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
